<template>
	<view class="my-path-list">
		<Lines></Lines>
		<view class="path-list">
			<view class="path-item" v-for="(item,index) in addressList" 
			:key='index'
			@click="itemActive(index)"
			:class="itemIndex===index?'activeItem':''"
			>
				<view class="item-text">
					<view class="text-name">{{item.userName}}</view>
					<view class="text-tel">{{item.tel}}</view>
				</view>
				<view class="item-address">
					<view class="address-defalut" v-show="item.isDefault==1?true:false">默认</view>
					<view class="address-name">{{item.address || item.province+item.city+item.district}}-{{item.details}}</view>
				</view>
				<view v-if="type==='updatepath'"  class="btns" v-show='itemIndex===index'>
					<view class="edit-btn" @click="goEditPath()"><image src="../../static/image/edit.png" mode=""></image></view>
					<view class="delete-btn"><image src="../../static/image/delete.png" mode="" @click="deletePathFn(item.id)"></image></view>
				</view>
				<view v-else class="radio-path">
					<label @click="backComfirmOrder(item,index)" class="radio">
						<radio value=""/>
					</label>
				</view>
			</view>
		</view>
		<view class="add-address" @click="goAddPath()">新增地址</view>
	</view>
</template>

<script>
	import $http from '../../common/api/request.js'
	import {mapState,mapMutations,mapGetters,mapActions} from 'vuex'
	import Lines from '../../components/common/Line.vue'
	export default {
		data() {
			return {
				// 当前item的index值
				itemIndex:-1,
				type:'updatepath',
			}
		},
		components:{
			Lines
		},
		computed:{
			...mapState({
				addressList:state=>state.path.list
			}),
			...mapGetters(['address'])
		},
		onLoad(e) {
			if(e.type){
				this.type=e.type
			}
			// 初始化拿到收货地址
			this.initAddress()
		},
		methods: {
			...mapActions(['deletePathFn']),
			...mapMutations(['__initAddress']),
			// 初始化
			initAddress(){
				$http.request({
					url: '/selectAddress',
					method:"post",
					header:{
						'content-type': 'application/x-www-form-urlencoded',
						token:true
					},
				}).then((res) =>{
					this.__initAddress(res)
				}).catch(() => {
					uni.showToast({
						title: '请求失败',
						icon: 'none'
					})
				})
			},
			// item选中方法
			itemActive(index){
				this.itemIndex=index
			},
			// 跳转新增页面
			goAddPath(){
				uni.navigateTo({
					url:'../my-add-path/my-add-path'
				})
			},
			// 跳转编辑页面
			goEditPath(){
				let pathObj=JSON.stringify({
					index:this.itemIndex,
					item:this.addressList[this.itemIndex]
				})
				uni.navigateTo({
					url:'../my-add-path/my-add-path?data='+pathObj+''
				})
			},
			// 在选择默认地址模式下，点击单选按钮会选择对应的数据传给订单确认页
			backComfirmOrder(item,index){
				uni.showToast({
					icon:'loading'
				})
				setTimeout(()=>{
					uni.$emit('selectDefaultPath',item)
					uni.navigateBack({
						delta:1
					})
				},100)
			}
		}
	}
</script>

<style scoped>
.path-item{
	position: relative;
	display: flex;
	flex-direction: column;
	margin-left: 40rpx;
	border-bottom: 1px solid #E1E1E1;
	padding:20rpx 20rpx 20rpx 10rpx;
}
.item-text{
	display: flex;
	padding-bottom: 10rpx;
}
.text-name{
	padding-right: 40rpx;
}
.item-address{
	display: flex;
	align-items: center;
}
.address-defalut{
	background-color: #00B7FF;
	height: 40rpx;
	width: 80rpx;
	line-height: 40rpx;
	font-size: 20rpx;
	color: #FFFFFF;
	text-align: center;
	border-radius: 20rpx;
	margin-right: 10rpx;
}
.address-name{
	font-size: 28rpx;
}
.add-address{
	margin: 20rpx auto;
	height: 80rpx;
	width: 360rpx;
	text-align: center;
	line-height: 80rpx;
	color: #00B7FF;
	border: 1px solid #00B7FF;
	border-radius:60rpx;
}
.btns{
	display: flex;
	position:absolute;
	top: 40rpx;
	right: 20rpx;
}
.btns image{
	width: 80rpx;
	height: 80rpx;
}
.edit-btn{
	padding-right: 20rpx;
}
.activeItem{
	box-shadow: inset 0 0 10rpx 0 rgba(0, 0, 0, 1);
	
}
.radio-path{
	position: absolute;
	right: 40rpx;
	top: 40rpx;
}
</style>
